package com.gkzf.ai.module.crm.service.creditreportsimple.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gkzf.ai.module.crm.dal.dataobject.creditreportsimple.UserAdmittanceConditionDO;
import com.gkzf.ai.module.crm.dal.dataobject.creditreportsimple.UserAdmittanceVehicleDO;
import com.gkzf.ai.module.crm.dal.mysql.creditreportsimple.UserAdmittanceVehicleMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.BiFunction;

/**
 * CRM 小程序补充信息-车辆 Service 实现类
 *
 * @author 国科智飞
 */
@Service
@Validated
public class AppUserAdmittanceVehicleServiceImpl implements AppUserAdmittanceVehicleService {

    @Resource
    private UserAdmittanceVehicleMapper userAdmittanceVehicleMapper;

    @Resource
    private AppUserAdmittanceConditionService appUserAdmittanceConditionService;


    @Override
    public void updateUserAdmittanceVehicle(List<UserAdmittanceVehicleDO> updateReqVO) {
        if (updateReqVO == null || updateReqVO.isEmpty()){
            return;
        }
        LambdaUpdateWrapper<UserAdmittanceVehicleDO> lambdaUpdate = new LambdaUpdateWrapper<>();
        lambdaUpdate.eq(UserAdmittanceVehicleDO::getUserOpenId, updateReqVO.get(0).getUserOpenId());

        List<UserAdmittanceVehicleDO> houseDOList = userAdmittanceVehicleMapper.selectList(lambdaUpdate);
        // 如果没有数据，则直接新增
        if(houseDOList.isEmpty()){
            userAdmittanceVehicleMapper.insertBatch(houseDOList);
            loanUses(updateReqVO, new ArrayList<>(), updateReqVO);
        }else {
            // 获取新增对象
            List<UserAdmittanceVehicleDO> addHouseList = updateReqVO.stream().filter(item -> Objects.isNull(item.getId())).toList();
            // 获取删除对象
            List<Long> noVoList = updateReqVO.stream().map(UserAdmittanceVehicleDO::getId).toList();
            List<UserAdmittanceVehicleDO> deleteHouseList = houseDOList.stream().filter(item -> !noVoList.contains(item.getId())).toList();
            // 获取更新对象
            List<Long> noDoList = houseDOList.stream().map(UserAdmittanceVehicleDO::getId).toList();
            List<UserAdmittanceVehicleDO> updateHouseList = updateReqVO.stream().filter(item -> noDoList.contains(item.getId())).toList();

            // 批量新增,删除,更新
            userAdmittanceVehicleMapper.updateBatch(updateHouseList);
            userAdmittanceVehicleMapper.insertBatch(addHouseList);
            userAdmittanceVehicleMapper.deleteByIds(deleteHouseList.stream().map(UserAdmittanceVehicleDO::getId).toList());
            loanUses(updateReqVO, updateHouseList, addHouseList);
        }
    }

    private void loanUses(List<UserAdmittanceVehicleDO> updateReqVO, List<UserAdmittanceVehicleDO> updateHouseList, List<UserAdmittanceVehicleDO> addHouseList) {
    /*
      如何房产不为空
   */
        if (!updateHouseList.isEmpty() || !addHouseList.isEmpty()){
            UserAdmittanceConditionDO condition = appUserAdmittanceConditionService.getUserAdmittanceCondition(updateReqVO.get(0).getUserOpenId());
            condition.setLoanUses(condition.getLoanUses().replace("15,","").replace("14,",""));
            Long fullPaymentCount = fullPayment.apply(updateHouseList, addHouseList);
            Long mortgageCount = mortgage.apply(updateHouseList, addHouseList);
            if (fullPaymentCount > 0){
                condition.setLoanUses(condition.getLoanUses() + "15,");
            }
            if (mortgageCount > 0){
                condition.setLoanUses(condition.getLoanUses() + "14,");
            }
            appUserAdmittanceConditionService.updateLoanUses(condition);
        }
    }


    /**
     * 全款房
     */
    public final BiFunction<List<UserAdmittanceVehicleDO>,List<UserAdmittanceVehicleDO>,Long> fullPayment = (updateHouseList, addHouseList) -> {
        long addCount = addHouseList.stream().filter(item -> item.getVehicleProperty() == 1).count();
        long updateCount = updateHouseList.stream().filter(item -> item.getVehicleProperty() == 1).count();
        return addCount+updateCount;
    };

    /**
     * 按揭房
     */
    public final BiFunction<List<UserAdmittanceVehicleDO>,List<UserAdmittanceVehicleDO>,Long> mortgage = (updateHouseList,addHouseList) -> {
        long addCount = addHouseList.stream().filter(item -> item.getVehicleProperty() == 2).count();
        long updateCount = updateHouseList.stream().filter(item -> item.getVehicleProperty() == 2).count();
        return addCount+updateCount;
    };

    @Override
    public void deleteUserAdmittanceVehicle(Long id) {

        // 删除
        userAdmittanceVehicleMapper.deleteById(id);
    }


    @Override
    public List<UserAdmittanceVehicleDO> getUserAdmittanceVehicleList(String userOpenId) {
        LambdaQueryWrapper<UserAdmittanceVehicleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAdmittanceVehicleDO::getUserOpenId, userOpenId);
        return userAdmittanceVehicleMapper.selectList(queryWrapper);
    }

}